package algorithm;
//Used in Sentinel Inc.'s Defensive Driver Prototype
//Programmer: John Casey Marr
//Programmer:
//Programmer: 
//Class CS 411 Professional Workforce Development
import gui.obu.ObuDrivingDisplayPanel;
import gui.obu.ObuDrivingDisplayPanelImpl;
import gui.obu.alerts.StopSignAlert;

import java.sql.SQLException;
import java.util.Calendar;

import database.OBUdata;
import database.OBUdb;

import objects.OBUentry;
import objects.StopSign;
import objects.StopSignLog;

public class Stop_Sign_Detection{

	//Current Location collected from OBU (Database)
	private double lat=1243.32;
	private double lon=1243.32;
	
	//Trigger and Event Identification Numbers
	private int trigger_ID;
	private int event_ID;
	private int box_ID;
	private int id1;
	private int id2;
	
	//Speed
	private int cur_speed;
	
	//Get Calendar Time
	private Calendar SS_now;
	private long ss_start;
	private long ss_stop;
	boolean stop_start;
	private boolean EventTriggered;
	private boolean Completed;
	private OBUentry myEntry;
	private StopSign BigRed;
	
	//Gui Stuff
	private ObuDrivingDisplayPanel window;
	
	public void setWindow(ObuDrivingDisplayPanel window){
		this.window=window;
	}
	
	public Stop_Sign_Detection()
	{
		id1=0;
		id2=0;
		lat=1243.32;
		lon=1243.32;
		trigger_ID=00000;
		event_ID=999999;
		box_ID=3333333;
		cur_speed=0;
		SS_now=Calendar.getInstance();
		ss_start=0;
		ss_stop=0;
		stop_start=false;
		EventTriggered=false;
		Completed=false;
		myEntry=new OBUentry();
		BigRed=new StopSign();
	}
	
	
	public void compute(OBUdata runtimedb, OBUdb obudatabase, Integer i,Integer driverID)
	{
		int newstopid = 0;

		//Step 1: Collect Location
		//        Database Call to get current location (OBU Database)
		//location=Database.get_Location();
		myEntry.setID(i);
		try {
			runtimedb.getOBUEntry(myEntry);
		} catch (java.sql.SQLException mysqlex) {
			System.out.println("Cannot get this entry");
			return;
		}
		lat=myEntry.getLat();
		lon=myEntry.getLong();
		
		//Step 2: Compare Location to Internal Stop Sign Database
		//box_ID=Database.get_Box(location)
		try {
			newstopid = obudatabase.detectStopSign(lat,lon);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("Could Not Get StopSign");
		}

		if (newstopid != -1)
		{
			System.out.println("New Stop Sign Box Detected is: " + newstopid);
			BigRed.setID(newstopid);
	                try {
				obudatabase.getStopSign(BigRed);
	                } catch (SQLException e) {
	                        // TODO Auto-generated catch block
	                        System.out.println("Could Not Get new Stop Sign's data");
	                }
			
			//Step 3: Determine if Box is a Trigger/Event/Neither
			//		  Trigger Box is identified by: 0 at the end of the ID
			//if(box_ID%10==0)
			//{
			//	trigger_ID=box_ID;
			//}
			//else //Event ID Found, check against trigger ID
			//{
			//	event_ID=box_ID;
			//	if((event_ID-trigger_ID)==1)//If Event has been triggered
			//	{
			//
		
			if(BigRed.getTriggerCheck())
			{
				trigger_ID=BigRed.getID();
			}
			else
			{
				event_ID=BigRed.getID();
			}
			
			//Check for speed to drop to 0 for 3 seconds in this box ID
			if((event_ID==(trigger_ID-1)) && !Completed)
			{
				if(!EventTriggered)
				{
				EventTriggered=true;
				SS_now.setTimeInMillis(System.currentTimeMillis());
				ss_start=SS_now.getTimeInMillis();
				
				
				}
				}


				if(EventTriggered)
				{

				if(myEntry.getSpeed()==0)
				{
				Completed=true;
				EventTriggered=false;
				}

				else
				{
				SS_now.setTimeInMillis(System.currentTimeMillis());
				ss_stop=SS_now.getTimeInMillis();
	
				if(ss_stop-ss_start>=4000)
				{

				StopSignLog  StopLog=new StopSignLog();
				StopLog.setStudentID(driverID);
				StopLog.setLocationID(event_ID);
				StopLog.setType(1);


				try{
				obudatabase.addStopSignLog(StopLog);
				}
				catch (java.sql.SQLException sqlex){
				System.out.println("Could not write Stop Sign Log");
				}
				
				if(window==null) {
					System.out.println("Could Not Add Alert to Window");
				} else {
					if(!((ObuDrivingDisplayPanelImpl)window).getDisplay().isDisposed()) {
						((ObuDrivingDisplayPanelImpl)window).getDisplay().asyncExec(new Runnable() {
							public void run() {
								((ObuDrivingDisplayPanelImpl)window).addAlert(new StopSignAlert());
							}
						});
					}
				}
				
				Completed=true;
				EventTriggered=false;
				}
				}

				}				
				
	}	
}}
